package com.pagepub.comm.util;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public final class FormatDateTime
{	
	public final static String DATE_YMD = "yyyy-MM-dd";
	public final static String DATE_YMDH = "yyyy-MM-dd HH";
	public final static String DATETIME_YMDHMS = "yyyy-MM-dd HH:mm:ss";
	public final static String DATETIME_YMDHM = "yyyy-MM-dd HH:mm";
	public final static String TIME_HMS = "HH:mm:ss";
	public final static String DefaultSmallTime = "00:00:00";
	public final static String DefaultBigTime = "23:59:59";
	
	private FormatDateTime(){}
	/**
	 * 
	 * @description    获取当前日期
	 * @return
	 */
	public static String getCurrDate()
	{
		return SimpleDateFormat.getDateInstance().format(new Date());
	}
	
	public static String getDateTime()
	{
		java.text.SimpleDateFormat f = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");			
		String time = f.format(new java.util.Date());
		return time;
	}
	
	/**
	 * @description    获取当前日期
	 * @return
	 */
	public static Date getCurrentDate() {
		String strDate = SimpleDateFormat.getDateInstance().format(new Date());
		Date date = null;
		try{
			date = new SimpleDateFormat("yyyy-MM-dd").parse(strDate);
		}catch(ParseException pe){
			pe.printStackTrace();
		}
		return date;
	}
	
	/**
	 * 获取当前时间
	 * @return
	 */
	public static Date getCurrentDateTime(){
		return string2DateTime2(getCurrDateTime());
	}
	
	/**
	 * 
	 * @description    获取当前时间
	 * @return
	 */
	public static String getCurrTime()
	{
		return SimpleDateFormat.getTimeInstance().format(new Date());
	}
	/**
	 * 
	 * @description    获取当前日期时间
	 * @return
	 */
	public static String getCurrDateTime()
	{
		return SimpleDateFormat.getDateTimeInstance().format(new Date());
	}
	/**
	 * 
	 * @description    字符型日期转换成日期型	 * @param ref_date
	 * @return
	 */
	public static Date string2Date(String ref_date)
	{	
		if(ref_date==null) return null;
		try
		{
			return new SimpleDateFormat(DATE_YMD).parse(ref_date);
		}
		catch(ParseException pe)
		{
			return null;
		}
	}
	/**
	 * 
	 * @description    字符型日期时间转换成日期型	 * @param ref_dt
	 * @return
	 */
	public static Date string2DateTime(String ref_dt)
	{
		try
		{
			return new SimpleDateFormat(FormatDateTime.DATETIME_YMDHMS).parse(ref_dt);
		}
		catch(ParseException pe)
		{
			return null;
		}
	}
	
	/**
	 * 字符型日期时间转换成日期型
	 * @param ref_dt
	 * @return
	 */
	public static Date string2DateTime2(String ref_dt)
	{
		try
		{
			return new SimpleDateFormat(FormatDateTime.DATETIME_YMDHM).parse(ref_dt);
		}
		catch(ParseException pe)
		{
			return null;
		}
	}
	
	/**
	 * @description    字符型日期时间转换成日期型	 * @param ref_date
	 * @param ref_format
	 * @return
	 */
	public static Date string2Date(String ref_date, String ref_format)
	{
		try
		{
			return new SimpleDateFormat(ref_format).parse(ref_date);
		}
		catch(Exception e)
		{
			return null;
		}
	}
	
	/**
	 * 
	 * @description    日期型日期转换成字符型	 * @param ref_date
	 * @return
	 */
	public static String date2String(Date ref_date)
	{
		return new SimpleDateFormat(DATE_YMD).format(ref_date);
	}
	/**
	 * 
	 * @description    日期型日期转换成指定格式字符型
	 * @author 庄志明   2006-8-29
	 * @param ref_date
	 * @param ref_format
	 * @return
	 */
	public static String date2String(Date ref_date, String ref_format)
	{
		try
		{
			return new SimpleDateFormat(ref_format).format(ref_date);
		}
		catch(Exception e)
		{
			return date2String(ref_date);
		}
	}
	/**
	 * 
	 * @description    日期型日期时间转换成字符型	 * @param ref_date
	 * @return
	 */
	public static String dateTime2String(Date ref_date)
	{
		return new SimpleDateFormat(DATETIME_YMDHMS).format(ref_date);
	}
	/**
	 * 
	 * @description    日期型日期时间转换成指定格式字符型	 * @param ref_date
	 * @param ref_format
	 * @return
	 */
	public static String dateTime2String(Date ref_date, String ref_format)
	{
		try
		{
			return new SimpleDateFormat(ref_format).format(ref_date);
		}
		catch(Exception e)
		{
			return dateTime2String(ref_date);
		}
	}
	 
	  
	 /**
	  * 
	  * @description:取得指定日期若干天后的日期	  * @param thisDate 指定日期
	  * @param addDays  若干天后
	  * @return
	  */
	   
	  public static String getNextDay(String thisDate, String addDays) {
	    Calendar cal = Calendar.getInstance();
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	    try {
	      Date date = formatter.parse(thisDate);
	      cal.setTime(date);
	      cal.add(Calendar.DATE, Integer.parseInt(addDays));
	      String nextDay = formatter.format(cal.getTime());
	      return nextDay;
	    }
	    catch (ParseException e) {
	      System.out.println("Unable to parse " + thisDate);
	      return "";
	    }
	  }
	  /**
	   * @description:取得指定日期若干月后的日期	   * @param thisDate
	   * @param addMonths
	   * @return
	   */
	  public static String getNextMonth(String thisDate, String addMonths) {
		    Calendar cal = Calendar.getInstance();
		    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		    try {
		      Date date = formatter.parse(thisDate);
		      cal.setTime(date);
		      cal.add(Calendar.MONTH, Integer.parseInt(addMonths));
		      String nextDay = formatter.format(cal.getTime());
		      return nextDay;
		    }
		    catch (ParseException e) {
		      System.out.println("Unable to parse " + thisDate);
		      return "";
		    }
		  }
	  public static int daysBetweenDate(Date date1, Date date2)
		{
			long days = Math.abs(date1.getTime() - date2.getTime());
			int int_days = (int)(days/(1000*60*60*24));
			return int_days;
		} 
	  
	  public static String getToday ( String pattern ){
	        Date date = new Date () ;
	        SimpleDateFormat sdf = new SimpleDateFormat ( pattern ) ;
	        return sdf.format ( date ) ;
	    }
	 
	/**
	 * test
	 */
	/*public static void test()
	{
		System.out.println(getCurrDate());
		System.out.println(getCurrTime());
		System.out.println(getCurrDateTime());
		System.out.println(string2Date("2006-08-19"));
		System.out.println(date2String(new Date()));
		System.out.println(date2String(new Date(),null));
		System.out.println(dateTime2String(new Date()));
		System.out.println(dateTime2String(new Date(),"MM/dd/yyyy HH:mm:ss"));
		System.out.println(string2DateTime("Wed Sep 20 14:26:11 CST 2006"));
		System.out.println(getNextDay("2006-10-31","1"));
		System.out.println("get current Date :"+getCurrentDate().toString());
		
	}
	*//**
	 * only for testing
	 * @param args
	 *//*
	public static void main(String[] args)
	{
		test();
	}*/
	  
	  /**
	  * 获取现在时间
	  *
	  * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
	 * @throws ParseException 
	  */
	  public static Date getNowDate() throws ParseException {
	  Date currentTime = new Date();
	  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	  String dateString = formatter.format(currentTime);
	  ParsePosition pos = new ParsePosition(8);
	  Date currentTime_2 = formatter.parse(dateString);	  
	  return currentTime_2;
	  }
	  
	  public static int getBetweenTwoDate(Date date1, Date date2)
		{
		  long   a1=date1.getTime();   
          long   a2=date2.getTime();   
          Date   date3=new   Date();   
          long   a3=(a1-a2)/1000;   
          //date3.setTime(a3);   
        
          int   nDay   =   (int)(a3/(24*60*60));   
          int   nHour   =   (int)(a3-nDay*24*60*60)/(60*60);   
          int   nMinute   =   (int)(a3-nDay*24*60*60-nHour*60*60)/60;   
          int   nSecond   =  (int) a3-nDay*24*60*60-nHour*60*60-nMinute*60;   
          System.out.println("间隔时间："+nDay+"天"+nHour+"小时"+nMinute+"分"+nSecond+"秒");
			return nSecond;
		} 
	  
	  public   static   void   main(String[]   args)   
      {   
              Date   date1=new Date(2008,10,10,15,54,24);   
              Date   date2=new Date(2008,10,10,13,44,36);   
              long   a1=date1.getTime();   
              long   a2=date2.getTime();   
              Date   date3=new   Date();   
              long   a3=(a1-a2)/1000;   
              //date3.setTime(a3);   
            
              int   nDay   =   (int)(a3/(24*60*60));   
              int   nHour   =   (int)(a3-nDay*24*60*60)/(60*60);   
              int   nMinute   =   (int)(a3-nDay*24*60*60-nHour*60*60)/60;   
              int   nSecond   =  (int) a3-nDay*24*60*60-nHour*60*60-nMinute*60;   
              System.out.println("间隔时间："+nDay+"天"+nHour+"小时"+nMinute+"分"+nSecond+"秒");
//              System.out.println(date3.toString());   
      }   
  

}
